package fr.sii.android.accessibility.model;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;

/**
 * save user's preferences
 * @author LoLo
 *
 */
public class Configuration {

	//private Boolean AppDir = false;
	
	/**
	 * default constructor
	 */
	public Configuration(){
		
	}
	
	/**
	 * Constructor
	 * @param fileName : file where save properties, to be created if it doesn't exist
	 * @throws Exception 
	 */
	public Configuration(String fileName) throws Exception{
		initClass(fileName);
	}
	
	/**
	 * Constructor
	 * @param fileName : file where save properties, to be created if it doesn't exist
	 * @param AppDirCalue : Indicate if file is in user home dir or app dir
	 * @throws Exception 
	 */
	/*
	public Configuration(String fileName, Boolean AppDirValue) throws Exception{
		AppDir = AppDirValue;
		initClass(fileName);
	}
	//*/

	/**
	 * Init class !
	 * @throws Exception 
	 */
	public void initClass(String fileName) throws Exception{
		String theFile = "";
		String theFolder = "";
		
		//if(AppDir) theFolder = System.getProperty("user.dir") + "\\Javaconfig\\";
		//else theFolder = System.getProperty("user.home") + "\\Javaconfig\\";
		
		theFolder = System.getProperty("user.dir") + "\\Javaconfig\\";
		
		theFile = theFolder + fileName;

		//If it doesn't exist, we create it
		File f = new File(theFile);
		if (!f.exists()) {

			File d = new File(theFolder);
			if (!d.exists()) {
				d.mkdirs();
			}
			f.createNewFile();
			
			//Set default data everywhere
			initConfig(fileName);
		}
	}
	
	
	/**
	 * Set default data everywhere (create keys with default or empty values)
	 * @throws Exception 
	 */
	public void initConfig(String fname) throws Exception{
			
		if(fname.equals("AVIP.properties")){
	
			//default : enable geolocalisation
			setConfig(fname, "Geolocalisation","Y");
		}
	}
	
    /**
    * Method    : getConfig
    *
    * Description :
    *   this method get properties entry from file name (in user home folder) 
    *
    * @param filename : name of properties file
    * @param key      : The key to obtain the value
    *
    * @return : String value of key
    * @throws Exception : Error on read file 
    */  
    public String getConfig(String filename, String key) throws Exception
    {
        //construct address of file
    	String theFile;
		//if(AppDir) theFile = System.getProperty("user.dir") + "\\Javaconfig\\" + filename;
		//else theFile = System.getProperty("user.home") + "\\Javaconfig\\" + filename;
    	
    	theFile = System.getProperty("user.dir") + "\\Javaconfig\\" + filename;
    	
        // Open the file
        FileInputStream fis = new FileInputStream(theFile);
        Properties config = new Properties();
        
        config.load(fis);
        
        String tmp = config.getProperty(key);
        fis.close();
        
        theFile = null;
        fis = null;
        config = null;
        
        if (tmp == null)
        {
            throw new Exception("Value corresponding to '" + key + "' does not exist in file '" + filename+"'");
        }
        return tmp;
    }
    
    /**
    * Method    : setConfig
    *
    * Description :
    *   This method set properties entry to file name (in user home folder)
    *   If file don't exist, it's created
    *
    * @param filename : name of properties file
    * @param key      : The key of the value to write
    * @param value    : Value of key
    * 
    * @throws Exception : Error on write file 
    *
    */  
    public void setConfig(String filename, String key, String value) throws Exception
    {
        //construct address of file
    	String theFile;
		//if(AppDir) theFile = System.getProperty("user.dir") + "\\Javaconfig\\" + filename;
		//else theFile = System.getProperty("user.home") + "\\Javaconfig\\" + filename;
    	theFile = System.getProperty("user.dir") + "\\Javaconfig\\" + filename;
    	
        // Read old file properties
        Properties config = new Properties();
        FileInputStream fis = new FileInputStream(theFile);
        config.load (fis);
        fis.close();
        // Write new file
        FileOutputStream fos = new FileOutputStream(theFile);
        config.setProperty(key,value);
        config.store (fos,"Last update :");
        fos.close();
        theFile = null;
        fos = null;
        fis = null;
        config = null;
    }
    
    /*
	public Boolean getAppDir() {
		return AppDir;
	}
	//*/
}
